package keye.document_manage.gui;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.TitledBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.filechooser.FileFilter;
import javax.swing.text.Document;

import keye.document_manage.util.MyConstants;

public class PushPanel extends JPanel{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField ruleTextField;
	private JTextField osPositionTextField;//cdg 2016年3月2日10:07:46 增加
	private JTextField logpathTextField;
	private JTextField txtFilingyearsqlWhere;
	private JTextField pushTitile;
	private JTextField newPath;
	private JButton checkButton;
	private JButton pushButton;
	private JCheckBox isCopyFiles;
	private JLabel totalSize;
	private JButton calculateSizeBtn;
	private JButton diySelectBtn;
//	private JCheckBox[] checkBtns = new JCheckBox[2];
	private JCheckBox checkInvalidImage;

	public JTextField getRuleTextField() {
		return ruleTextField;
	}
	
	public PushPanel() {
		init();
		initEvents();
	}
	public void init(){
		this.setBounds(300, 0, 100, getWidth());
		this.setLayout(new GridLayout(0, 2, 0, 0));
		
		JPanel panel_2 = new JPanel();
		panel_2.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u6821\u9A8C\u9009\u9879", TitledBorder.LEFT, TitledBorder.TOP, null, null));
		this.add(panel_2);
		panel_2.setLayout(new BoxLayout(panel_2, BoxLayout.Y_AXIS));
		
		JPanel panel_4 = new JPanel();
		FlowLayout flowLayout = (FlowLayout) panel_4.getLayout();
		flowLayout.setVgap(0);
		flowLayout.setHgap(3);
		flowLayout.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_4);
		
		Component verticalStrut = Box.createHorizontalStrut(20);
		panel_4.add(verticalStrut);
		JLabel label = new JLabel("原文存储规则：");
		panel_4.add(label);
		
		ruleTextField = new JTextField();
		panel_4.add(ruleTextField);
		ruleTextField.setColumns(50);
		
		//cdg 2016年3月2日10:10:20 add
		JPanel panel_9 = new JPanel();
		FlowLayout flowLayout_5 = (FlowLayout) panel_9.getLayout();
		flowLayout_5.setVgap(0);
		flowLayout_5.setHgap(3);
		flowLayout_5.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_9);
		
		panel_9.add(Box.createHorizontalStrut(20));
		JLabel label_5 = new JLabel("原文所在目录：");
		panel_9.add(label_5);
		//"M:\\test"
		osPositionTextField = new JTextField();
		panel_9.add(osPositionTextField);
		osPositionTextField.setColumns(50);
		
		JButton button_4 = new JButton(new ImageIcon(MyConstants.PICTURE_PATH+"f3.png"));
		button_4.addActionListener(e->{
			osPositionTextField.setText(openFileChooser(JFileChooser.DIRECTORIES_ONLY));
		});
		panel_9.add(button_4);
		//end
		
		JPanel panel_5 = new JPanel();
		panel_2.add(panel_5);
		panel_5.setLayout(new GridLayout(2, 2, 0, 0));
		
		JPanel panel_7 = new JPanel();
		FlowLayout flowLayout_1 = (FlowLayout) panel_7.getLayout();
		flowLayout_1.setVgap(0);
		flowLayout_1.setHgap(0);
		flowLayout_1.setAlignment(FlowLayout.LEFT);
		panel_5.add(panel_7);
		
		Component horizontalStrut = Box.createHorizontalStrut(20);
		panel_7.add(horizontalStrut);
		
		JCheckBox checkBox = new JCheckBox("检查是否有误挂接目录");
		panel_7.add(checkBox);
		
		JPanel panel_8 = new JPanel();
		FlowLayout flowLayout_2 = (FlowLayout) panel_8.getLayout();
		flowLayout_2.setVgap(0);
		flowLayout_2.setHgap(0);
		flowLayout_2.setAlignment(FlowLayout.LEFT);
		panel_5.add(panel_8);
		
		Component horizontalStrut_1 = Box.createHorizontalStrut(20);
		panel_8.add(horizontalStrut_1);
		
		checkInvalidImage = new JCheckBox("检查原文是否存在无效图片");
		panel_8.add(checkInvalidImage);
//		checkInvalidImage.set
		
		JPanel panel_6 = new JPanel();
		FlowLayout flowLayout_3 = (FlowLayout) panel_6.getLayout();
		flowLayout_3.setVgap(0);
		flowLayout_3.setHgap(3);
		flowLayout_3.setAlignment(FlowLayout.LEFT);
		panel_2.add(panel_6);
		
		Component horizontalStrut_2 = Box.createHorizontalStrut(20);
		panel_6.add(horizontalStrut_2);
		
		JLabel label_1 = new JLabel("校验日志路径：");
		panel_6.add(label_1);
		//"M:\\test\\ttt\\log.txt"
		logpathTextField = new JTextField();
		panel_6.add(logpathTextField);
		logpathTextField.setColumns(50);
		
		JButton button_1 = new JButton(new ImageIcon(MyConstants.PICTURE_PATH+"f3.png"));
		panel_6.add(button_1);
		button_1.addActionListener(e->{
			logpathTextField.setText(openFileChooser(JFileChooser.FILES_ONLY,".txt"));
		});
		
//		JPanel panel_9 = new JPanel();
//		FlowLayout flowLayout_4 = (FlowLayout) panel_9.getLayout();
//		flowLayout_4.setVgap(0);
//		flowLayout_4.setHgap(10);
//		flowLayout_4.setAlignment(FlowLayout.RIGHT);
//		panel_2.add(panel_9);
		
		Component horizontalStrut_8 = Box.createHorizontalStrut(120);
		panel_6.add(horizontalStrut_8);
		
		checkButton = new JButton("校验");
		panel_6.add(checkButton);
		
		JPanel panel_3 = new JPanel();
		panel_3.setBorder(new TitledBorder(UIManager.getBorder("TitledBorder.border"), "\u53D1\u5E03\u9009\u9879", TitledBorder.LEFT, TitledBorder.TOP, null, null));
		this.add(panel_3);
		panel_3.setLayout(new BoxLayout(panel_3, BoxLayout.Y_AXIS));
		
		JPanel panel_10 = new JPanel();
		FlowLayout flowLayout_6 = (FlowLayout) panel_10.getLayout();
		flowLayout_6.setHgap(0);
		flowLayout_6.setAlignment(FlowLayout.LEFT);
		flowLayout_6.setVgap(0);
		panel_3.add(panel_10);
		
		Component horizontalStrut_3 = Box.createHorizontalStrut(20);
		panel_10.add(horizontalStrut_3);
		
		setIsCopyFiles(new JCheckBox("发布原文"));
		panel_10.add(getIsCopyFiles());
		
		totalSize = new JLabel("原文大小： 0 KB");
		panel_10.add(totalSize);
		
		calculateSizeBtn = new JButton("检查原文大小");
		calculateSizeBtn.setActionCommand("checkSize");
		panel_10.add(calculateSizeBtn);
		
		JPanel panel_11 = new JPanel();
		FlowLayout flowLayout_7 = (FlowLayout) panel_11.getLayout();
		flowLayout_7.setHgap(0);
		flowLayout_7.setVgap(0);
		flowLayout_7.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_11);
		
		Component horizontalStrut_4 = Box.createHorizontalStrut(20);
		panel_11.add(horizontalStrut_4);
		
		JLabel label_2 = new JLabel("自定义条件：");
		panel_11.add(label_2);
		
		txtFilingyearsqlWhere = new JTextField();
		txtFilingyearsqlWhere.setText("and filingyear>2011(SQL where 语句)");
		panel_11.add(txtFilingyearsqlWhere);
		txtFilingyearsqlWhere.setColumns(50);
		
		diySelectBtn = new JButton("自定义查询");
		diySelectBtn.setEnabled(false);
		panel_11.add(diySelectBtn);
		
		JPanel panel_12 = new JPanel();
		FlowLayout flowLayout_8 = (FlowLayout) panel_12.getLayout();
		flowLayout_8.setVgap(0);
		flowLayout_8.setHgap(0);
		flowLayout_8.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_12);
		
		Component horizontalStrut_5 = Box.createHorizontalStrut(20);
		panel_12.add(horizontalStrut_5);
		
		JLabel label_3 = new JLabel("  发布标题：");
		panel_12.add(label_3);
		
		pushTitile = new JTextField();
		pushTitile.setText("011-2011-永久_"+new SimpleDateFormat("yyyyMMddHHmm").format(new Date()));
		panel_12.add(pushTitile);
		pushTitile.setColumns(50);
		
		JPanel panel_13 = new JPanel();
		FlowLayout flowLayout_9 = (FlowLayout) panel_13.getLayout();
		flowLayout_9.setVgap(0);
		flowLayout_9.setHgap(0);
		flowLayout_9.setAlignment(FlowLayout.LEFT);
		panel_3.add(panel_13);
		
		Component horizontalStrut_6 = Box.createHorizontalStrut(20);
		panel_13.add(horizontalStrut_6);
		
		JLabel label_4 = new JLabel("  发布路径：");
		panel_13.add(label_4);
		
		//"M:"+File.separator+"test"+File.separator+"okok2"
		newPath = new JTextField();
		panel_13.add(newPath);
		newPath.setColumns(50);
		
		JButton button_3 = new JButton(new ImageIcon(MyConstants.PICTURE_PATH+"f3.png"));
		button_3.addActionListener(e->{
			newPath.setText(openFileChooser(JFileChooser.DIRECTORIES_ONLY));
		});
		panel_13.add(button_3);
		
		Component horizontalStrut_7 = Box.createHorizontalStrut(150);
		panel_13.add(horizontalStrut_7);
		
		setPushButton(new JButton("发布"));
		panel_13.add(getPushButton());
	}

	public void initEvents(){
		Document doc = txtFilingyearsqlWhere.getDocument();
		doc.addDocumentListener(new DocumentListener() {
			@Override
			public void removeUpdate(DocumentEvent e) {
				System.out.println(1);
				if(doc.getLength()>0)
					diySelectBtn.setEnabled(true);
				else
					diySelectBtn.setEnabled(false);
			}
			@Override
			public void insertUpdate(DocumentEvent e) {
				System.out.println(2);
				diySelectBtn.setEnabled(true);
			}
			@Override
			public void changedUpdate(DocumentEvent e) {
				System.out.println(3);
				diySelectBtn.setEnabled(true);
			}
		});
	}
	
	protected void diySelectChange(){
//		txtFilingyearsqlWhere.addv
		
	}
	
	private String openFileChooser(int mode,String... types){
		JFileChooser jfc = new JFileChooser();
		jfc.setFileFilter(new FileFilter() {
			@Override
			public String getDescription() {
				StringBuilder sb = new StringBuilder();
				for (int i = 0; i < types.length; i++) {
					sb.append(";"+types[i]);
				}
				if(sb.length()>0)
					sb.deleteCharAt(0);
				return sb.toString();
			}
			
			@Override
			public boolean accept(File f) {
				if(f.isDirectory())
					return true;
				String name = f.getName().toLowerCase();
				for (int i = 0; i < types.length; i++) {
					if(name.endsWith(types[i]))
						return true;
				}
				return false;
			}
		});
//		jfc.is
		jfc.setFileSelectionMode(mode );  
        jfc.showDialog(new JLabel(), "选择");  
        File file=jfc.getSelectedFile();
        if(file!=null){
        	return file.getAbsolutePath();
        }
        return "";
	}
	
	public JButton getCheckButton() {
		return checkButton;
	}

	public void setCheckButton(JButton checkButton) {
		this.checkButton = checkButton;
	}

	public JButton getPushButton() {
		return pushButton;
	}

	public void setPushButton(JButton pushButton) {
		this.pushButton = pushButton;
	}

	public JCheckBox getIsCopyFiles() {
		return isCopyFiles;
	}

	public void setIsCopyFiles(JCheckBox isCopyFiles) {
		this.isCopyFiles = isCopyFiles;
	}
	
	public JTextField getNewPath() {
		return newPath;
	}
	
	public JLabel getTotalSize() {
		return totalSize;
	}
	public JTextField getPushTitile() {
		return pushTitile;
	}
	public JButton getCalculateSizeBtn() {
		return calculateSizeBtn;
	}
	public JButton getDiySelectBtn() {
		return diySelectBtn;
	}
	public JTextField getTxtFilingyearsqlWhere() {
		return txtFilingyearsqlWhere;
	}
	public JTextField getLogpathTextField() {
		return logpathTextField;
	}
//	public JCheckBox[] getCheckBtns() {
//		return checkBtns;
//	}
	public JCheckBox getCheckInvalidImage() {
		return checkInvalidImage;
	}
	
	public JTextField getOsPositionTextField() {
		return osPositionTextField;
	}
}
